
//////////////////////////////////////////////////////////////////////////////////
//	GestureDetection.cs
//	
//	Written by Cosic Nadin and Franz Thomas Reinisch FH-Joanneum ITM06
//  
//////////////////////////////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Text;
using WiimoteLib;
using System.Timers;



namespace WiiFlipChart
{
    class GestureDetection
    {
        private bool gestureActionPossible = true;
        private System.Timers.Timer timer;


        static readonly GestureDetection instance = new GestureDetection();


        public static GestureDetection Instance
        {
            get
            {
                return instance;
            }
        }


      


        /// <summary>
        /// Constructor
        /// init a timer
        /// subscribe the WiimoteChangedEventHandler
        /// </summary>
        public GestureDetection()
        {
            
            
            //Timer to limit the amount of gesture Action  
            InitTimer(); 
           
   
            //Subscribe WiimoteChangedEventHandler
            WiiMoteDetection.Instance.WiimoteChanged += new WiimoteChangedEventHandler(WiimoteChangedEvent);
        
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void WiimoteChangedEvent(object sender, WiimoteChangedEventArgs args)
        {
            WiimoteState ws = args.WiimoteState;

            if (ws.ButtonState.B)
            {
               
                double ax, ay, az;

                az = Math.Round(ws.AccelState.Z, 3);
                ax = Math.Round(ws.AccelState.X, 3);
                ay = Math.Round(ws.AccelState.Y, 3);

        
                
                if (ax > -0.2 && ax < 0.2 &&    ay < (-0.2) &&  ay > (-0.7)   &&    az < (-0.5) && az > (-1.9) && gestureActionPossible == true)
                {
                    ColorControl.Instance.changecurrentColor();
                    
                    
                    gestureActionPossible = false; timer.Start();
                }


               //Change Stroke Width 
                if (ax < -0.9  &&          az < (+1.1) && az > (+0.9)        && gestureActionPossible == true)
                {
     
                        StrokeWidthControl.Instance.changecurrentStrokeWidth();

                        gestureActionPossible = false; timer.Start();
                    
                }



                
                //Go to next site
                if (ax > (+0.7) && ax < (+1) &&   ay > (-0.2) && ay < (0.2)   &&     az > (-0.2) && az < (+0.6)    &&    gestureActionPossible == true)
                {

                      PageControl.Instance.showNextPage();

                        gestureActionPossible = false; timer.Start();
               
              
                }

                
                // Go to previous site
                if (  ax < (-0.7) && ax > (-0.9) &&    ay > (-0.2) && ay < (0.2)      && az > (-0.2) && az < (+0.5) &&  gestureActionPossible == true)
                {

                    if (gestureActionPossible == true)
                    {
                       PageControl.Instance.showPrevPage();

                       gestureActionPossible = false; timer.Start();
                    }
                }
                
                 

                

            }
        
        }

        
        /// <summary>
        /// Make it possible to do a gestureaction again
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnTimerTickEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer.Stop();
            gestureActionPossible = true;
            
        }

        /// <summary>
        /// This timer is to prevent to many gestures in a short time
        /// </summary>
        private void InitTimer()
        {
            timer = new System.Timers.Timer();
            timer.Interval = 500;
            timer.Elapsed += new ElapsedEventHandler(this.OnTimerTickEvent);

        }


    }
}
